﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Business;

namespace University.Workshop.Presenters.Cistern.Register
{
    public class PresenterRegisterCistern
    {
        IRegisterBasicCisternView viewRegisterBasic;
        Model.Cistern newCistern = new Model.Cistern() { Person = new Model.Person() };
        IRegisterPrincipalView view;
        public PresenterRegisterCistern(IRegisterPrincipalView View)
        {
            view = View;
            newCistern = new Model.Cistern() { Person = new Model.Person(),LicencePlate= string.Empty};        
            
        }


        public void GoStepTwo()
        {
            view.RegisterBasic.ChargeDataToBasic();
            newCistern = view.RegisterBasic.CisternToRegister;
            if (!PassFirstStep())
                view.CallFirstPage();
            else
                view.CallSecondPage();
            ProcedureAfterCall();
        }

        public void GoStepOne()
        {
            view.RegisterBasic.ChargeDataToBasic();
            newCistern = view.RegisterBasic.CisternToRegister;
            view.CallFirstPage();
            ProcedureAfterCall();
        }
        public void RegisterCistern()
        {
            view.RegisterBasic.ChargeDataToBasic();
            newCistern = view.RegisterBasic.CisternToRegister;
            if (!PassFirstStep())
            {
                view.CallFirstPage();
                ProcedureAfterCall();
            }
            else if (!PassSecondStep())
            {
                view.CallSecondPage();
                ProcedureAfterCall();
            }
            else
            {
                ManagerCistern.NewCistern(newCistern);
                view.SystemMessage("La cisterna Fue Registrada Exitosamente.!!!!");
                view.RestartRegisterCistern();
            }
        }

        private bool PassSecondStep()
        {

            try
            {
                Model.Cistern mp = new Model.Cistern() { Person = new Model.Person() };
                mp.Person.PersonId = view.RegisterBasic.CisternToRegister.Person.PersonId;
                mp.Person.Names = view.RegisterBasic.CisternToRegister.Person.Names;
                mp.Person.LastNames = view.RegisterBasic.CisternToRegister.Person.LastNames;
                mp.Person.Phone = view.RegisterBasic.CisternToRegister.Person.Phone;

                ManagerCistern.VerifyDataPerson(mp);
                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(ex.Message);
                return false;
            }
        }

        private bool PassFirstStep()
        {
            try
            {
                Model.Cistern mp = new Model.Cistern();
                mp.Capacity = view.RegisterBasic.CisternToRegister.Capacity;
                mp.LicencePlate = view.RegisterBasic.CisternToRegister.LicencePlate;
                ManagerCistern.VerifyDataCisternIsCorrect(mp);
                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(ex.Message);
                return false;
            }
        }

        

        public void GoBackPage()
        {
            viewRegisterBasic.GoBackPage();
        }

        public void GoNextPage()
        {
            viewRegisterBasic.GoNextPage();
        }

        private void ProcedureAfterCall()
        {

            if (viewRegisterBasic != null)
                newCistern = viewRegisterBasic.CisternToRegister;
            viewRegisterBasic = view.RegisterBasic;
            viewRegisterBasic.CisternToRegister = newCistern;
            viewRegisterBasic.Update();
        }

        public void FirstTime()
        {
            view.CallFirstPage();
            ProcedureAfterCall();
        }

        public void NotifyErrorMessage(string MessageValue)
        {
            view.ErrorMessage(MessageValue);
        }
        public void NotifySystemMessage(string MessageValue)
        {
            view.SystemMessage(MessageValue);
        }
    }
}
